//
//  MTFavoriteViewCell.m
//  ShiShiBang
//
//  Created by loujianwei on 16/9/26.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTFavoriteViewCell.h"
#import <Masonry.h>
#import "MTFavoriteItem.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface MTFavoriteViewCell ()

@property (nonatomic, strong) UIImageView *bigImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *resumeLabel;
@property (nonatomic, strong) UIImageView *promulgatorimage;
@property (nonatomic, strong) UILabel *nameLabel;

@end
@implementation MTFavoriteViewCell

- (void)setFrame:(CGRect)frame {
    frame.size.height -= 11;
    [super setFrame:frame];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        UIImageView *bigImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:bigImageView];
        self.bigImageView = bigImageView;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textColor = [UIColor blueColor];
        [titleLabel sizeToFit];
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UILabel *resumeLabel = [[UILabel alloc] init];
        resumeLabel.font = [UIFont systemFontOfSize:12];
        resumeLabel.numberOfLines = 0;
        resumeLabel.lineBreakMode = NSLineBreakByCharWrapping;
        resumeLabel.textColor = MTColor(187, 187, 187);
        [resumeLabel sizeToFit];
        [self.contentView addSubview:resumeLabel];
        self.resumeLabel = resumeLabel;
        
        UIImageView *promulgatorimage = [[UIImageView alloc] init];
        promulgatorimage.layer.cornerRadius = promulgatorimage.mt_width * 0.5;
        promulgatorimage.layer.masksToBounds = YES;
        [self.contentView addSubview:promulgatorimage];
        self.promulgatorimage = promulgatorimage;
        
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = [UIFont systemFontOfSize:10];
        nameLabel.textColor = [UIColor lightGrayColor];
        [nameLabel sizeToFit];
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        UIImageView *logImageView = [[UIImageView alloc] init];
        logImageView.image = [UIImage imageNamed:@"Btn_Collection"];
        [self.contentView addSubview:logImageView];
        
        [bigImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(15);
            make.left.equalTo(self.mas_left).offset(19);
            make.width.equalTo(@(79.5));
            make.height.equalTo(@(79.5));
        }];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bigImageView.mas_top);
            make.left.equalTo(bigImageView.mas_right).offset(15);
            make.height.equalTo(@(20));
        }];
        
        [resumeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel.mas_left);
            make.top.equalTo(titleLabel.mas_bottom).offset(10);
            make.right.equalTo(self.mas_right).offset(-30);
            make.height.equalTo(@(50));
        }];
        
        [promulgatorimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(19);
            make.top.equalTo(bigImageView.mas_bottom).offset(10);
            make.width.equalTo(@(22));
            make.height.equalTo(@(22));
        }];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(promulgatorimage.mas_centerY);
            make.left.equalTo(promulgatorimage.mas_right).offset(10);
            make.height.equalTo(@(20));
        }];
        
        [logImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-20);
            make.bottom.equalTo(self.mas_bottom).offset(-20);
            make.width.equalTo(@(20));
            make.height.equalTo(@(20));
        }];
        
        //        //开启上下文
//        UIGraphicsBeginImageContextWithOptions(promulgatorimage.frame.size, NO, 0.0);
//        //画圆：正切于上下文
//        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, promulgatorimage.frame.size.width, promulgatorimage.frame.size.height)];
//        //设为裁剪区域
//        [path addClip];
//        //画图片
//        [self.image drawAtPoint:CGPointZero];
//        //生成一个新的图片
//        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//        // 关闭上下文
//        UIGraphicsEndImageContext();
        
//        promulgatorimage sd_setImageWithURL:<#(NSURL *)#> placeholderImage:<#(UIImage *)#>
    }
    return self;
    
}

- (void)setItem:(MTFavoriteItem *)item {
    _item = item;

    self.bigImageView.image = [UIImage imageNamed:item.bigImage];
    self.nameLabel.text = item.nameLabel;
    self.titleLabel.text = [NSString stringWithFormat:@"#%@#",item.titleLabel];
    self.resumeLabel.text = item.resumeLabel;
    self.promulgatorimage.image = [UIImage imageNamed:item.promulgatorimage];
}

@end
